package com.ruoyi.project.system.leave.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.course.domain.CourseArrange;
import com.ruoyi.project.system.course.service.ICourseArrangeService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.leave.domain.Leave;
import com.ruoyi.project.system.leave.service.ILeaveService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 学生请假 信息操作处理
 * 
 * @author Waynnn
 * @date 2019-04-07
 */
@Controller
@RequestMapping(value = "/system/leave")
public class LeaveController extends BaseController
{
    private String prefix = "system/leave";
	
	@Autowired
	private ILeaveService leaveService;

	@Autowired
	private ICourseArrangeService courseArrangeService;
	
	@RequiresPermissions("system:leave:view")
	@GetMapping()
	public String leave(ModelMap mmap)
	{
		User user=getSysUser();
		mmap.put("teacherName",user.getUserName());
		List<Role> roles=user.getRoles();
		for (Role role:roles){
			if (role.getRoleName().equals("管理员")){
				return prefix+ "/leaveForAdmin";
			}
		}
		CourseArrange courseArrange=new CourseArrange();
		courseArrange.setTeacherId(user.getUserId());
		List<CourseArrange> courseArranges=courseArrangeService.selectCourseArrangeList(courseArrange);
		String teacherCourse="";
		for (CourseArrange courseArrange1:courseArranges){
			teacherCourse=teacherCourse+courseArrange1.getCourse().getCourseName()+",";
		}
		if (teacherCourse.equals("")){
			teacherCourse="尚未安排课程";
		}
		mmap.put("teacherCourse",teacherCourse);
	    return prefix + "/leave";
	}
	
	/**
	 * 查询学生请假列表
	 */
	@RequiresPermissions("system:leave:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Leave leave)
	{
		startPage();
        List<Leave> list = leaveService.selectLeaveList(leave);
		return getDataTable(list);
	}

	/**
	 * 查询学生请假列表
	 */
	@RequiresPermissions("system:leave:list")
	@PostMapping("/listForAdmin")
	@ResponseBody
	public TableDataInfo listForAdmin(Leave leave)
	{
		startPage();
		List<Leave> list = leaveService.selectLeaveListForAdmin(leave);
		return getDataTable(list);
	}
	

	
	/**
	 * 新增学生请假
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存学生请假
	 */
	@RequiresPermissions("system:leave:add")
	@Log(title = "学生请假", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Leave leave)
	{		
		return toAjax(leaveService.insertLeave(leave));
	}

	/**
	 * 修改学生请假
	 */
	@GetMapping("/edit/{leaveId}")
	public String edit(@PathVariable("leaveId") Long leaveId, ModelMap mmap)
	{
		Leave leave = leaveService.selectLeaveById(leaveId);
		String leaveTimeStr=DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",leave.getLeaveTime());
		mmap.put("leave", leave);
		mmap.put("leaveTimeStr",leaveTimeStr);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存学生请假
	 */
	@RequiresPermissions("system:leave:edit")
	@Log(title = "学生请假", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Leave leave)
	{		
		return toAjax(leaveService.updateLeave(leave));
	}
	
	/**
	 * 删除学生请假
	 */
	@RequiresPermissions("system:leave:remove")
	@Log(title = "学生请假", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(leaveService.deleteLeaveByIds(ids));
	}
	
}
